﻿/*
    Jarloo
    http://jarloo.com
 
    This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License  
    http://creativecommons.org/licenses/by-sa/3.0/     

*/
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Threading;
using System.Xml.Linq;
using Jarloo.CardStock.Helpers;
using Jarloo.CardStock.Models;

namespace Jarloo.CardStock.ViewModels
{
    public class CardDeckViewModel : DependencyObject
    {
        private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Background);

        public ObservableCollection<Quote> Quotes { get; set; }

        public CardDeckViewModel()
        {
            Quotes = new ObservableCollection<Quote>();

            string url = "http://www.batstrading.com/market_data/symbol_listing/xml/";
             XDocument doc = XDocument.Load(url);
             var symbols = (from s in doc.Root.Element("symbols").Elements("symbol")
                            select new { Name = s.Attribute("name").Value }).ToList();
            for (int i = 0; i < 10; i++)
                Quotes.Add(new Quote(symbols[i].Name));
            //Some example tickers
            /* Quotes.Add(new Quote("AAPL"));
             Quotes.Add(new Quote("MSFT"));
             Quotes.Add(new Quote("INTC"));
             Quotes.Add(new Quote("IBM"));
             Quotes.Add(new Quote("RVBD"));
             Quotes.Add(new Quote("AMZN"));
             Quotes.Add(new Quote("BIDU"));
             Quotes.Add(new Quote("SINA"));
             Quotes.Add(new Quote("THI"));
             Quotes.Add(new Quote("NVDA"));
             Quotes.Add(new Quote("AMD"));
             Quotes.Add(new Quote("DELL"));
             Quotes.Add(new Quote("WMT"));
             Quotes.Add(new Quote("GLD"));
             Quotes.Add(new Quote("SLV"));
             Quotes.Add(new Quote("V"));
             Quotes.Add(new Quote("V"));
             Quotes.Add(new Quote("MCD"));*/
            Quotes.Add(new Quote("EPAM"));

            //get the data
            YahooStockEngine.Fetch(Quotes);

            //poll every 25 seconds
            timer.Interval = new TimeSpan(0, 0, 25); 
            timer.Tick += (o, e) => YahooStockEngine.Fetch(Quotes);
                              
            timer.Start();
        }
    }
}